package com.ddpt.permission.dubbo;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.center.common.base.BizEntity;
import com.lh.center.common.dto.PageDto;
import com.lh.center.permission.api.dubbo.DubboBackAdminUserService;
import com.lh.center.permission.api.entity.BackAdminUser;
import com.lh.center.permission.api.request.BackAdminAccountRequest;
import com.lh.center.permission.api.request.BackAdminQueryAccountRequest;
import com.lh.center.permission.api.request.BackAdminShowNameBatchQueryRequest;
import com.lh.center.permission.api.response.BackAdminAccountResponse;
import com.lh.center.permission.api.response.BackAdminUserResponse;
import com.lh.center.permission.mapper.BackAdminUserMapper;
import com.lh.center.permission.service.BackAdminUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class DubboBackAdminUserServiceImpl implements DubboBackAdminUserService {

    @Resource
    private BackAdminUserMapper backAdminUserMapper;

    @Resource
    private BackAdminUserService backAdminUserService;


    @Override
    public List<BackAdminUserResponse> queryBackAdminShowName(List<Long> backAdminUserIds) {
        BackAdminShowNameBatchQueryRequest request = new BackAdminShowNameBatchQueryRequest();
        request.setBackAdminUserIds(backAdminUserIds);
        return backAdminUserMapper.queryBackAdminShowName(request);
    }

    @Override
    public Page<BackAdminAccountResponse> queryBackAdminAccountPageList(BackAdminQueryAccountRequest queryRequest) {
        PageDto dto = new PageDto(Long.valueOf(queryRequest.getCurrent()).intValue(),Long.valueOf(queryRequest.getSize()).intValue());
        Page<BackAdminUser> page = backAdminUserService.page(dto.toMp(), Wrappers.lambdaQuery(BackAdminUser.class)
                .like(StringUtils.isNotBlank(queryRequest.getName()),BackAdminUser::getShowName,queryRequest.getName())
                .eq(queryRequest.getStatus()!=null,BackAdminUser::getStatus,queryRequest.getStatus())
        );
        Page<BackAdminAccountResponse> res = new Page<>();
        List<BackAdminUser> records = page.getRecords();
        page.setRecords(null);
        BeanUtils.copyProperties(page,res);
        if(records.isEmpty()){
            return res;
        }
        res.setRecords(records.stream().map(item -> {
            BackAdminAccountResponse response = new BackAdminAccountResponse();
            BeanUtils.copyProperties(item,response);
            return response;
        }).collect(Collectors.toList()));
        return res;
    }

    @Override
    public void saveOrUpdate(BackAdminAccountRequest request) {
        BackAdminUser backAdminUser = new BackAdminUser();
        BeanUtils.copyProperties(request, backAdminUser);
        //密码加密存储
        backAdminUser.setPassword(DigestUtil.md5Hex(request.getPassword()));
        //密码为空则从数据库中找
        if (ObjectUtil.isEmpty(request.getPassword())) {
            String password = backAdminUserService.get(Wrappers.lambdaQuery(BackAdminUser.class).eq(BackAdminUser::getId, request.getId())).getPassword();
            backAdminUser.setPassword(password);
        }
        backAdminUserService.saveOrUpdate(backAdminUser);
    }

    @Override
    public void del(Long id) {
        LambdaUpdateWrapper<BackAdminUser> wrapper = Wrappers.lambdaUpdate(BackAdminUser.class).eq(BizEntity::getId, id).set(BackAdminUser::getRoleId, null);
        backAdminUserService.update(wrapper);
    }

    @Override
    public void updateStatus(BackAdminAccountRequest request) {
        BackAdminUser backAdminUser = new BackAdminUser();
        backAdminUser.setId(request.getId());
        backAdminUser.setStatus(request.getStatus());
        backAdminUserService.updateById(backAdminUser);
    }
}
